﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using System.Data;
using Model;
using Utils;

namespace Website.uc
{
    public partial class ucBuyItem : System.Web.UI.UserControl
    {
        ItemsDao itemDao = new ItemsDao();
        AuctionSessionDao auctionSessionDao = new AuctionSessionDao();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadItemInfo();
            }
        }

        public void LoadItemInfo()
        {
            if (Session["id"] != null)
            {
                DataTable tb = itemDao.getItemById(Session["id"].ToString());
                if (tb.Rows.Count > 0)
                {
                    lblDes.Text = tb.Rows[0]["ItemDescription"].ToString();
                    lblIncre.Text = tb.Rows[0]["BidIncremenent"].ToString();
                    lblMini.Text = tb.Rows[0]["Minimumbid"].ToString();
                    lblStatus.Text = tb.Rows[0]["BidStatus"].ToString();
                    lblEndDate.Text = tb.Rows[0]["BidEndDate"].ToString();
                    lblTitle.Text = tb.Rows[0]["ItemTitle"].ToString();
                    lblCate.Text = tb.Rows[0]["BidStatus"].ToString();
                    lblStartDate.Text = tb.Rows[0]["BidStartDate"].ToString();
                    Image1.ImageUrl = tb.Rows[0]["ItemImage"].ToString();
                }

            }
        }

        protected void btnBuy_Click(object sender, EventArgs e)
        {
            bool flag = false;
            AuctionSession auctionSession = new AuctionSession();
            if(CheckValidate.CheckNull(txtPrice.Text)){
                Utility.ShowMessage("Enter price !!" , Page);
                txtPrice.Focus();
            }
            else if (!CheckValidate.IsInteger(txtPrice.Text))
            {
                Utility.ShowMessage("Enter price invalid !!", Page);
                txtPrice.Focus();
            }
            else
            {
                DataTable tb = auctionSessionDao.checkBuy(Session["id"].ToString(),
                                                     Session["userId"].ToString());

                if (tb.Rows.Count > 0)
                {
                    Utility.ShowMessage("You have bought this product , please select orther product !!", Page);
                }
                else
                {
                    auctionSession.BuyerId = Int32.Parse(Session["userId"].ToString());
                    auctionSession.ItemId = Int32.Parse(Session["id"].ToString());
                    auctionSession.PriceOffer = txtPrice.Text;
                    auctionSession.IsWinner = "Pending";
                    flag = auctionSessionDao.insertAuctionSession(auctionSession);
                    if (flag)
                    {
                        Utility.ShowMessage("Buy product successfull !!", Page);
                        txtPrice.Text = "";
                    }
                }

            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("Home.aspx");
        }
    }
}